package nl.altenpts.net.kad;

import java.util.Comparator;
import java.math.BigInteger;

public class DistanceComparator implements Comparator<Node> {

	DistanceComparator(Id root) {
		this.id = root;
	}
	
	public int compare(Node object1, Node object2) {
		
		if(object1 == object2) {
			return 0;
		}
		
		if(object1 == null)  {
			return -1;
		} else if (object2 == null) {
			return 1;
		}
		
		byte dist1[] = object1.getId().getDistance(id);
		byte dist2[] = object2.getId().getDistance(id);
		
		BigInteger int1 = new BigInteger(1, dist1);
		BigInteger int2 = new BigInteger(1, dist2);
		
		return int1.compareTo(int2);
	}

	private Id id;
}
